┌──[root@liruilongs.github.io]-[~] └─$ip netns list ┌──[root@liruilongs.github.io]-[~] └─$ip netns add red ┌──[root@liruilongs.github.io]-[~] └─$ip netns add green
用于在 Linux 上创建一个名为 vnet-br0 的桥接设备。桥接设备是用于连接多个网络设备的虚拟设备。它可以实现数据包的转发和交换,使得连接到桥接设备的网络设备可以相互通信。
1 2
┌──[root@liruilongs.github.io]-[~] └─$iplink add vnet-br0 type bridge
桥接设备(Bridge Device)是在网络层次结构中工作的二层设备(Data Link Layer),它主要用于连接多个网络设备,类似于网络交换机的功能。桥接设备通过学习和转发数据帧的方式,将连接到它的网络设备组成一个共享的以太网段,使得这些设备可以直接通信。桥接设备工作在数据链路层(第二层),它不涉及 IP 地址或路由。
通过 ip link 查看设备的状态、属性和配置信息
1 2 3 4 5 6 7 8 9
┌──[root@liruilongs.github.io]-[~] └─$iplink 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:93:51:67 brd ff:ff:ff:ff:ff:ff altname enp3s0 5: vnet-br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether ce:93:3b:6d:37:48 brd ff:ff:ff:ff:ff:ff
可以看到刚刚添加的虚拟桥接设备,目前处于 DOWN 状态。
添加虚拟网卡对eth0-r 和 veth-r、eth0-g 和 veth-g
1 2 3 4
┌──[root@liruilongs.github.io]-[~] └─$iplink add eth0-r type veth peer name veth-r ┌──[root@liruilongs.github.io]-[~] └─$iplink add eth0-g type veth peer name veth-g
把两个虚拟网卡对中的一端放到上面创建的网络命名空间
1 2 3 4
┌──[root@liruilongs.github.io]-[~] └─$iplinkset eth0-r netns red ┌──[root@liruilongs.github.io]-[~] └─$iplinkset eth0-g netns green
┌──[root@liruilongs.github.io]-[~] └─$iplink show type bridge 5: vnet-br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff
查看桥接设备(vnet-br0)关联的网络设备。
1 2 3 4 5 6
┌──[root@liruilongs.github.io]-[~] └─$iplink show master vnet-br0 6: veth-r@if7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vnet-br0 state DOWN mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff link-netns red 8: veth-g@if9: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vnet-br0 state DOWN mode DEFAULT group default qlen 1000 link/ether be:a3:9a:1c:a1:06 brd ff:ff:ff:ff:ff:ff link-netns green
根据输出,有两个网络设备与 vnet-br0 桥接设备关联:
veth-r@if7:这是一个虚拟网络设备(veth pair),它与 vnet-br0 桥接设备关联。它的状态是 DOWN,表示当前处于未激活状态。它的 MAC 地址为 62:2b:41:f9:39:b3。此设备属于 red 网络命名空间。
veth-g@if9:这是另一个虚拟网络设备(veth pair),也与 vnet-br0 桥接设备关联。它的状态是 DOWN,表示当前处于未激活状态。它的 MAC 地址为 be:a3:9a:1c:a1:06。此设备属于 green 网络命名空间。
激活桥接对应的网络设备
1 2 3 4 5 6
┌──[root@liruilongs.github.io]-[~] └─$iplinkset vnet-br0 up ┌──[root@liruilongs.github.io]-[~] └─$iplinkset veth-r up ┌──[root@liruilongs.github.io]-[~] └─$iplinkset veth-g up
激活 网络命名空间中的回环地址和对应的虚拟网卡对
1 2 3 4 5 6 7 8
┌──[root@liruilongs.github.io]-[~] └─$ip netns exec red ip linkset lo up ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec red ip linkset eth0-r up ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec green ip linkset lo up ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec green ip linkset eth0-g up
ip link 确认设备状态
1 2 3 4 5 6 7 8 9 10 11 12 13
┌──[root@liruilongs.github.io]-[~] └─$iplink 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:93:51:67 brd ff:ff:ff:ff:ff:ff altname enp3s0 5: vnet-br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff 6: veth-r@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master vnet-br0 state UP mode DEFAULT group default qlen 1000 link/ether 62:2b:41:f9:39:b3 brd ff:ff:ff:ff:ff:ff link-netns red 8: veth-g@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master vnet-br0 state UP mode DEFAULT group default qlen 1000 link/ether be:a3:9a:1c:a1:06 brd ff:ff:ff:ff:ff:ff link-netns green
┌──[root@liruilongs.github.io]-[~] └─$ip netns exec red ping 192.168.26.149 -c 3 PING 192.168.26.149 (192.168.26.149) 56(84) bytes of data. 64 bytes from 192.168.26.149: icmp_seq=1 ttl=64 time=0.241 ms 64 bytes from 192.168.26.149: icmp_seq=2 ttl=64 time=0.110 ms 64 bytes from 192.168.26.149: icmp_seq=3 ttl=64 time=0.075 ms
--- 192.168.26.149 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2074ms rtt min/avg/max/mdev = 0.075/0.142/0.241/0.071 ms ┌──[root@liruilongs.github.io]-[~] └─$ip netns exec green ping 192.168.26.149 -c 3 PING 192.168.26.149 (192.168.26.149) 56(84) bytes of data. 64 bytes from 192.168.26.149: icmp_seq=1 ttl=64 time=0.258 ms 64 bytes from 192.168.26.149: icmp_seq=2 ttl=64 time=0.097 ms 64 bytes from 192.168.26.149: icmp_seq=3 ttl=64 time=0.094 ms
┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$iplink show type bridge 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT link/ether 02:42:68:f8:90:26 brd ff:ff:ff:ff:ff:ff ┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$iplink show master docker0 ┌──[root@vms100.liruilongs.github.io]-[~/ansible] └─$